package com.yuelan.apartment.controller;

import com.yuelan.apartment.domain.ApaApartment;
import com.yuelan.apartment.service.ApaApartmentService;
import com.yuelan.common.annotation.Log;
import com.yuelan.common.core.controller.BaseController;
import com.yuelan.common.core.domain.AjaxResult;
import com.yuelan.common.core.page.TableDataInfo;
import com.yuelan.common.enums.BusinessType;
import com.yuelan.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description: 房源管理
 * @Author: ZhaoYi
 * @date: 24-5-18 10:53
 */
@RestController
@RequestMapping(value = "/house")
public class ApaApartmentController extends BaseController {

    @Autowired
    private ApaApartmentService apaApartmentService;


    @PostMapping
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房源添加", businessType = BusinessType.INSERT)
    public AjaxResult addApartment(@Validated @RequestBody ApaApartment apaApartment){
        return toAjax(apaApartmentService.add(apaApartment));
    }


    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房源删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(apaApartmentService.deleteIds(ids));
    }


    @PutMapping
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "房源编辑", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody @Validated ApaApartment apaApartment) {
        return toAjax(apaApartmentService.update(apaApartment));
    }


    @GetMapping(value = "/{id}")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(apaApartmentService.load(id));
    }


    @GetMapping("/list")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    public TableDataInfo list(ApaApartment apaApartment) {
        startPage();
        List<ApaApartment> list = apaApartmentService.pageList(apaApartment);
        return getDataTable(list);
    }


    @PostMapping("/export")
    @PreAuthorize("@ss.hasAnyRoles('admin,common')")
    @Log(title = "导出房源信息列表", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, ApaApartment apaApartment) {
        List<ApaApartment> list = apaApartmentService.pageList(apaApartment);
        ExcelUtil<ApaApartment> util = new ExcelUtil<>(ApaApartment.class);
        util.exportExcel(response, list, "房源信息数据");
    }

}
